$(function() {
		
		// Dialog
		var name = $( "#name" ),
			allFields = $( [] ).add( name ),
			tips = $( ".validateTips" ),
			single=true;
	   
		$( "#selectPerson-dialog" ).dialog({
			autoOpen: false,
			height: 380,
			minHeight: 380,
			width: 380,
			minWidth: 380,
			modal: true,
			draggable: false,
			close: function() {
						allFields.val( "" ).removeClass( "ui-state-error" );
						$( "#selectPerson-dialog" ).children().remove();
					},
			buttons: [{
					id:"cancel",
					tabindex:3,
					text:"Annuler",
					click: function() {
								$( this ).dialog( "close" );
								$( "#selectPerson-dialog" ).children().remove();
							}
				},{
					id:"submit-selectPerson",
					tabindex:2,
					text:"Déléguer",
					click: function() {
						
						var bValid = true;
						allFields.removeClass( "ui-state-error" );

						bValid = bValid && checkLength( tips, name, "Nom", 3, 40 );
						//bValid = bValid && checkLength( tips, email, "email", 6, 80 );
						//bValid = bValid && checkLength( tips, password, "password", 5, 16 );

						//bValid = bValid && checkRegexp( tips, name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );
						// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
						//bValid = bValid && checkRegexp( tips, email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@jquery.com" );
						//bValid = bValid && checkRegexp( tips, password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );

						if ( bValid&&single ) {
							single=false;
							$("#personCode").val($("#code").val());
							$( "#selectPerson-dialog" ).append("<div id='progress'><br/><p class='progressbar'>Délégation en cours..</p><div id='progressbar' class='progressbar'/></div></div>");
							
							$( "#progressbar" ).progressbar({
								value: 100
							});
							
							$.ajax({
								type: 'POST',
								dataType:'json',
								url: "delegate.htm",
								data: $("#delegationForm").serialize(),
								error:function(msg){
									tips.text('error').addClass('ui-state-error ui-corner-all');
									$( "#progress" ).remove();
									single=true;
								},
								success: function( data ) {
									single=true;
									if(data.success==true){
										$( "#selectPerson-dialog"  ).dialog( "close" );
										$( "#notification-reload-dialog" ).append("<p style='text-align=center;'><img src='/ASOF/img/success.png' style='width:20px ;position: absolute; left: .5em; top: 13%;margin-top: -8px;' /><span style='position: absolute;left:30px'>"+data.message+"</span></p>");
										$( "#notification-reload-dialog" ).dialog("open");
									}else{
										$( "#selectPerson-dialog" ).dialog( "close" );
										
										if(data.reload){				
											$( "#notification-reload-dialog" ).append("<div id='validateTips'>"+data.message+"</div>");
											$( "#notification-reload-dialog" ).dialog("open");
							
										}else{
											$( "#notification-dialog" ).append("<div id='validateTips'>"+data.message+"</div>");
											$( "#notification-dialog" ).dialog("open");
										}
										$("#validateTips").addClass('ui-state-error ui-corner-all');
										$( "#progress" ).remove();
									}
										
										
								}
							});
							
						}
					}
				}
			]
	   });
	   
		// Autocomplete
		$( "#name" ).autocomplete({
			source: function( request, response ) {
				$.ajax({
					url: "autocomplete.htm",
					dataType: "jsonp",
					data: {
						featureClass: "P",
						style: "full",
						maxRows: 12,
						name_startsWith: request.term
					},
					success: function( data ) {
						response( $.map( data.persons, function( item ) {
							return {
								label: item.personFirstName+" "+item.personLastName+" ( "+item.personId+" )",
								value: item.personFirstName+" "+item.personLastName,
								id :item.personCode
								
							};
						}));
					}
				});
			},
			minLength: 2,
			select: function( event, ui ) {
				$("#code").val(ui.item.id);
			},
			open: function() {
				$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
			},
			close: function() {
				$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
			}
		});
});